import { InsertResult } from 'typeorm'
import configuration from 'src/config/configuration'
import WXBizDataCrypt from './WXBizDataCrypt'
export function prefixUrl<T>(arg: T): T | string {
  return `/${process.env.PREFIX1}/${arg}`
}

/**
 * @description:根据数组中的id去重
 * @param {Array} arr:需要去重的数组
 * @param {string} id:去重的关键id
 * @return {*}:返回新数组
 */
export function filterObject<T>(arr: Array<T>, id: string = 'id'): Array<T> {
  let arrayMap = new Map()
  arr.forEach(item => {
    arrayMap.set(item[id], item)
  })
  return [...arrayMap.values()]
}

// 将增删改查结果改一下
export function changeResultToString<T>(result: T) {
  if (result instanceof InsertResult) {
    return {
      msg: `新增了${result.raw.affectedRows}条数据`,
      result,
    }
  } else {
    return {
      msg: `成功`,
      result,
    }
  }
}

export function decryptData(encryptedData, iv, session_key, appId) {
  // var WXBizDataCrypt = require('./WXBizDataCrypt')

  var pc = new WXBizDataCrypt(appId, session_key)

  var data = pc.decryptData(encryptedData, iv)

  return data
}
